package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"image/png"
	"os"

	"gitee.com/yejia/radar/analysis"
	"gitee.com/yejia/radar/data"
	"gitee.com/yejia/radar/draw"
	"gitee.com/yejia/radar/swan"
)

func main() {
	// parse() // 提供路径解析雷达产品数据
	drawSwanImg()
}

func parse() {
	radarpath := "D:/ZV602/Z_RADR_I_ZV602_20250415070007_P_DOR_YLD1-D_CR_75X75_194_NUL_FMT.bin.bz2"
	rw, err := analysis.Parse(radarpath)
	if err != nil {
		panic(err)
	}
	fmt.Printf("%+v\nlen:%d", rw.GenericHeader, len(rw.Data))
}

func drawImg() {
	file, err := os.ReadFile("./draw/color.json")
	if err != nil {
		panic(err)
	}
	var colorJsonData [][]int
	err = json.Unmarshal(file, &colorJsonData)
	if err != nil {
		panic(err)
	}

	radarpath := "D:/ZV602/Z_RADR_I_ZV602_20250415070007_P_DOR_YLD1-D_CR_75X75_194_NUL_FMT.bin.bz2"
	rw, err := analysis.Parse(radarpath)
	if err != nil {
		panic(err)
	}

	img := draw.DrawImg(colorJsonData, *rw)
	fmt.Printf("%+v\n", rw.TaskConfiguration)
	fmt.Printf("%+v\n", rw.ProductHeader)
	fmt.Printf("%+v\n", rw.ProductRasterParams)
	// 将image.RGBA的像素数据转换为[]byte
	buf := new(bytes.Buffer)
	png.Encode(buf, img)
	f, err := os.Create("./test.png")
	if err != nil {
		panic(err)
	}
	defer f.Close()
	f.Write(buf.Bytes())
}

func drawSwanImg() {
	colorJsonData := data.GetColorMap("./data/colormap/REF_s.cmap")

	radarpath := "E:/works/wenerzc/duanlin/radad131/yubao/Z_OTHE_RADAMCR_RFST_20250522063600.120.bin"
	rw, err := swan.Parse(radarpath)
	if err != nil {
		panic(err)
	}

	fmt.Printf("swanHeader:%+v\n", rw.SwanHeader)
	img := draw.DrawSwanImg(colorJsonData, *rw)
	fmt.Printf("X_grid_num:%d\n", rw.SwanHeader.X_grid_num)
	fmt.Printf("Y_grid_num:%d\n", rw.SwanHeader.Y_grid_num)
	fmt.Printf("Z_grid_num:%d\n", rw.SwanHeader.Z_grid_num)
	var maxValue, minValue float64
	for _, v := range rw.Data {
		for _, c := range v {
			if c == nil {
				continue
			}
			if maxValue < *c {
				maxValue = *c
			}
			if minValue > *c {
				minValue = *c
			}
		}
	}
	fmt.Printf("maxValue:%f minValue:%f\n", maxValue, minValue)
	// 将image.RGBA的像素数据转换为[]byte
	buf := new(bytes.Buffer)
	png.Encode(buf, img)
	f, err := os.Create("./test.png")
	if err != nil {
		panic(err)
	}
	defer f.Close()
	f.Write(buf.Bytes())
}
